package com.example.administrator.activity.Homework.Homework6.Homework6_1;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;

public class SDHomeWork6 {
    public static boolean save(Context context, String ht, String wt) {
        String device=Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File sdPath=Environment.getExternalStorageDirectory();
            File file=new File(sdPath,"homework6_1.txt");
            FileOutputStream outputStream;
            BufferedWriter writer;
            try {
                outputStream=new FileOutputStream(file);
                writer=new BufferedWriter(new OutputStreamWriter(outputStream));
                writer.write(ht+":"+wt);
                writer.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public static HashMap<String, String> get(Context context) {
        HashMap<String,String> hashMap=new HashMap<String, String>();
        String device=Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File sdPath=Environment.getExternalStorageDirectory();
            File file=new File(sdPath,"homework6_1.txt");
            FileInputStream inputStream=null;
            BufferedReader reader;
            try {
                inputStream=new FileInputStream(file);
                reader=new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line=reader.readLine())!=null){
                    String[] s=line.split(":");
                    hashMap.put("ht",s[0]);
                    hashMap.put("wt",s[1]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }
}
